package com.fgs.common.widget.itemView;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.fgs.common.R;
import com.fgs.common.utils.CommonUtil;
import com.fgs.common.widget.spinner.PullSpinner;
import com.xuexiang.xui.utils.ResUtils;

import java.util.List;


/**
 * @author fgs
 * @date 2020/3/29
 * @description
 **/
public class PullItemView extends LinearLayout {
    private PullSpinner mPullSpinner;
    private int mEntriesID;
    private String mTitle;
    private int mTitleTextColor;
    private TextView mTitleTextView;

    public PullItemView(Context context) {
        super(context);
        initView(context, null);
    }

    public PullItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.PullItemView);
        int dropdownMaxHeight;
        try {
            mTitle = typedArray.getString(R.styleable.PullItemView_PullItemTitle);
            dropdownMaxHeight = typedArray.getInt(R.styleable.PullItemView_PullItemDropdownMaxHeight,200);
            mTitleTextColor = typedArray.getColor(R.styleable.PullItemView_PullItemTitleTextColor, context.getResources().getColor(R.color.colorTextBlack));
            mEntriesID = typedArray.getResourceId(R.styleable.PullItemView_PullItemViewEntries, 0);
        } finally {
            typedArray.recycle();
        }


        View view = LayoutInflater.from(context).inflate(R.layout.layout_pull_item, null);
        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(layoutParams);
        addView(view);

        mPullSpinner = view.findViewById(R.id.layout_pull_item_pullSpinner);
        mTitleTextView = view.findViewById(R.id.layout_pull_item_titleTextView);
        if (mEntriesID != 0) {
            setItems(ResUtils.getStringArray(mEntriesID));
        }
        setTitle(mTitle);
        setTitleTextColor(mTitleTextColor);
        setDropdownMaxHeight(dropdownMaxHeight);
    }

    public void setTitle(String title) {
        this.mTitle = title;
        mTitleTextView.setText(title);
    }

    public void setTitleTextColor(int titleTextColor) {
        this.mTitleTextColor = titleTextColor;
        mTitleTextView.setTextColor(titleTextColor);
    }

    /**
     * Set the maximum height of the dropdown menu.
     *
     * @param height the height in pixels
     */
    public void setDropdownMaxHeight(int height) {
        mPullSpinner.setDropdownMaxHeight((int) CommonUtil.convertDpToPixel(height));
    }

    /**
     * Register a callback to be invoked when an item in the dropdown is selected.
     *
     * @param onItemSelectedListener The callback that will run
     */
    public PullItemView setOnItemSelectedListener(@Nullable PullSpinner.OnItemSelectedListener onItemSelectedListener) {
        mPullSpinner.setOnItemSelectedListener(onItemSelectedListener);
        return this;
    }


    /**
     * 设置选中的内容
     *
     * @param item 选中的内容
     * @param <T>
     */
    public <T> PullItemView setSelectedItem(@NonNull T item) {
        mPullSpinner.setSelectedItem(item);
        return this;
    }

    /**
     * 获取选中内容在Spinner中的位置
     *
     * @param item  选中的内容
     * @param items Spinner中选项的集合
     * @return
     */
    public <T> int getSpinnerPosition(T item, List<T> items) {
        return mPullSpinner.getSpinnerPosition(item, items);
    }

    /**
     * Set the dropdown items
     *
     * @param items A list of items
     * @param <T>   The item type
     */
    public <T> PullItemView setItems(@NonNull T... items) {
        mPullSpinner.setItems(items);
        return this;
    }

    /**
     * Set the dropdown items
     *
     * @param items A list of items
     * @param <T>   The item type
     */
    public <T> PullItemView setItems(@NonNull List<T> items) {
        mPullSpinner.setItems(items);
        return this;
    }

    /**
     * Get the list of items in the adapter
     *
     * @param <T> The item type
     * @return A list of items or {@code null} if no items are set.
     */
    public <T> List<T> getItems() {
        return mPullSpinner.getItems();
    }

    /**
     * Get the Selected Item in the adapter
     *
     * @param <T> The item type
     * @return A list of items or {@code null} if no items are set.
     */
    public <T> T getSelectedItem() {
        return mPullSpinner.getSelectedItem();
    }

}
